/**
 * 通用更新
 * @url admin/system/base/update
 * @description 通用更新操作
 */
module.exports = {
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: '更新成功' };
		
		// 表名映射配置
		const dbNameMap = {
			user: 'uni-id-users',
			role: 'uni-id-roles',
			permission: 'uni-id-permissions',
			menu: 'opendb-admin-menus'
		};
		
		// 业务逻辑开始-----------------------------------------------------------
		let { type, _id, ...updateData } = data;
		
		if (!type || !dbNameMap[type]) {
			return { code: -1, msg: '类型参数错误' };
		}
		
		if (!_id) {
			return { code: -1, msg: 'ID不能为空' };
		}
		
		let dbName = dbNameMap[type];
		
		// 过滤掉 undefined 的值
		let filteredData = {};
		for (let key in updateData) {
			if (updateData[key] !== undefined) {
				filteredData[key] = updateData[key];
			}
		}
		
		if (Object.keys(filteredData).length === 0) {
			return { code: -1, msg: '没有要更新的数据' };
		}
		
		// 更新数据
		await db.collection(dbName).doc(_id).update(filteredData);
		
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}

